package org.study.easyes.boot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.study.easyes.boot.entity.Product;
import org.study.easyes.boot.service.ProductService;

import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2022-10-25
 */
@Slf4j
@Controller
public class ProductSearchController {
    @Autowired
    private ProductService productService;

    @PostMapping("/add1")
    public String addProduct1(Product product, Model model) {
        int flag = productService.addProduct(product);
        model.addAttribute("flag", "addProduct is success!!!" + flag);
        return "/index";
    }

    @PostMapping("/add2")
    public String addProduct2(Product product, Model model) {
        int flag = productService.addProduct(product);
        model.addAttribute("flag", "addProduct is success!!!" + flag);
        return "/index";
    }

    @RequestMapping("/search")
    public ModelAndView searchProduct(@RequestParam(value = "name") String name) {
        ModelAndView modelAndView = new ModelAndView();
        List<Product> list = productService.searchByName(name);
        modelAndView.addObject("list", list);
        modelAndView.setViewName("/index");
        return modelAndView;
    }

    @RequestMapping("/delete/{id}")
    public String deleteProduct(@PathVariable("id") String id, Model model) {
        int flag = productService.deleteProduct(id);
        model.addAttribute("flag", "deleteProduct is success!!!" + flag);
        return "/index";
    }
}
